package com.net.orders.controller;

import com.caikun.model.common.PageResult;
import com.caikun.model.common.Result;
import com.caikun.model.dto.OrderPageDTO;
import com.caikun.model.dto.OrderSubmitDTO;
import com.caikun.model.entity.Order;
import com.caikun.model.vo.OrderTimeVo;
import com.caikun.model.vo.OrderVo;
import com.net.orders.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 订单表(Order)表控制层
 *
 * @author makejava
 * @since 2025-07-17 08:53:20
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    /**
     * 服务对象
     */
    @Autowired
    private OrderService orderService;

    /**
     * 分页查询
     * @param orderPageDTO
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping("/page")
    @Operation(summary = "分页查询订单信息")
    public Result<PageResult> queryByPage(@RequestBody OrderPageDTO orderPageDTO, int pageNum, int pageSize) {
        PageResult pageResult = this.orderService.queryByPage(orderPageDTO, pageNum, pageSize);
        return Result.success(pageResult);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/search/{id}")
    @Operation(summary = "查询订单信息")
    public Result<OrderVo> queryById(@PathVariable("id") Integer id) {
        return Result.success(this.orderService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param order 实体
     * @return 新增结果
     */
    @PostMapping("/add")
    @Operation(summary = "新增订单")
    public Result<Order> add(@RequestBody Order order) {
        return Result.success(this.orderService.insert(order));
    }

    /**
     * 编辑数据
     *
     * @param order 实体
     * @return 编辑结果
     */
    @PutMapping("/edit")
    @Operation(summary = "更新订单信息")
    public Result<Order> edit(@RequestBody Order order) {
        return Result.success(this.orderService.update(order));
    }

    /**
     * 删除数据（逻辑删除）
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/delete")
    @Operation(summary = "逻辑删除订单")
    public Result<Boolean> deleteById(Integer id) {
        return Result.success(this.orderService.deleteById(id));
    }

    /**
     * 提交订单（从购物车获取订单数据）
     * @param
     * @return
     */
    @PostMapping("/submit")
    @Operation(summary = "提交订单")
    public Result<Map<String, Object>> submit(@RequestBody OrderSubmitDTO orderSubmitDTO) {
        return Result.success(this.orderService.submit(orderSubmitDTO));
    }

    /**
     * 查询用户订单
     * @param userId
     * @return
     */
    @GetMapping("/queryByUserId")
    @Operation(summary = "查询用户订单")
    public Result<List<OrderVo>> queryByUserId(Integer userId) {
        return Result.success(this.orderService.queryByUserId(userId));
    }

    /**
     * 查询用户进行中的订单
     * @param userId
     * @return
     */
    @GetMapping("/getUserOrderByStatus")
    @Operation(summary = "查询用户进行中订单")
    public Result<OrderTimeVo> getUserOrderByStatus(Integer userId) {
        return Result.success(this.orderService.getUserOrderByStatus(userId));
    }

    /**
     * 设置订单结束时间
     * @param orderId
     *
     * @return
     */
    @PutMapping("/setOverTime")
    @Operation(summary = "设置订单结束时间")
    public Result<Boolean> setOverTime(Integer orderId) {
        return Result.success(this.orderService.setOverTime(orderId));
    }

    /**
     * 设置订单状态
     * @param orderId
     * @param status
     * @return
     */
    @PutMapping("/setOrderStatus")
    @Operation(summary = "设置订单状态")
    public Result<Boolean> setOrderStatus(Integer orderId, Integer status) {
        return Result.success(this.orderService.setOrderStatus(orderId, status));
    }

}

